export const getformdata=(tableData)=>{
  // 1. 初始化累加变量（默认0，避免undefined）
  let sharpElectricity = 0;      // 尖电量
  let peakElectricity = 0;       // 峰电量
  let flatElectricity = 0;       // 平电量
  let valleyElectricity = 0;     // 谷电量
  let totalElectricity = 0;      // 总电量

  let sharpElectricityPrice = 0; // 尖电费
  let peakElectricityPrice = 0;  // 峰电费
  let flatElectricityPrice = 0;  // 平电费
  let valleyElectricityPrice = 0;// 谷电费
  let totalElectricityPrice = 0; // 总电费


  // 2. 循环累加表格数据
  tableData.forEach(item => {
    // 累加电量（处理null/undefined，转为0）
    sharpElectricity += Number(item.sharpElectricity || 0);
    peakElectricity += Number(item.peakElectricity || 0);
    flatElectricity += Number(item.flatElectricity || 0);
    valleyElectricity += Number(item.valleyElectricity || 0);
    totalElectricity += Number(item.totalElectricity || 0);

    // 累加电费
    sharpElectricityPrice += Number(item.sharpElectricityPrice || 0);
    peakElectricityPrice += Number(item.peakElectricityPrice || 0);
    flatElectricityPrice += Number(item.flatElectricityPrice || 0);
    valleyElectricityPrice += Number(item.valleyElectricityPrice || 0);
    totalElectricityPrice += Number(item.totalElectricityPrice || 0);
  });


  // 3. 工具函数：不四舍五入保留两位小数（核心逻辑）
  const keepTwoDecimals = (num) => {
    // 先转为字符串，按小数点分割，取整数部分和小数部分前两位
    const numStr = num.toString();
    const decimalIndex = numStr.indexOf('.');
    if (decimalIndex === -1) {
      return `${numStr}.00`; // 无小数部分，补.00
    }
    // 有小数部分：整数部分 +. + 小数部分前两位（不足补0）
    const integerPart = numStr.slice(0, decimalIndex);
    const decimalPart = numStr.slice(decimalIndex + 1, decimalIndex + 3).padEnd(2, '0');
    return `${integerPart}.${decimalPart}`;
  };


  // 4. 计算占比（先保留多位小数，再处理两位小数）
  // 电量占比 = 单项电量 / 总电量 * 100
  const sharpUsageRate = totalElectricity!== 0 
   ? (sharpElectricity / totalElectricity) * 100 
    : 0;
  const peakUsageRate = totalElectricity!== 0 
   ? (peakElectricity / totalElectricity) * 100 
    : 0;
  const flatUsageRate = totalElectricity!== 0 
   ? (flatElectricity / totalElectricity) * 100 
    : 0;
  const valleyUsageRate = totalElectricity!== 0 
   ? (valleyElectricity / totalElectricity) * 100 
    : 0;

  // 电费占比 = 单项电费 / 总电费 * 100
  const sharpPriceRate = totalElectricityPrice!== 0 
   ? (sharpElectricityPrice / totalElectricityPrice) * 100 
    : 0;
  const peakPriceRate = totalElectricityPrice!== 0 
   ? (peakElectricityPrice / totalElectricityPrice) * 100 
    : 0;
  const flatPriceRate = totalElectricityPrice!== 0 
   ? (flatElectricityPrice / totalElectricityPrice) * 100 
    : 0;
  const valleyPriceRate = totalElectricityPrice!== 0 
   ? (valleyElectricityPrice / totalElectricityPrice) * 100 
    : 0;


  // 5. 组装formdata并返回（所有值处理为字符串，确保两位小数）
  return {
    // 电量相关
    sharpUsage: keepTwoDecimals(sharpElectricity),
    peakUsage: keepTwoDecimals(peakElectricity),
    flatUsage: keepTwoDecimals(flatElectricity),
    valleyUsage: keepTwoDecimals(valleyElectricity),
    totalElectricity: keepTwoDecimals(totalElectricity),

    // 电费相关
    sharpPrice: keepTwoDecimals(sharpElectricityPrice),
    peakPrice: keepTwoDecimals(peakElectricityPrice),
    flatPrice: keepTwoDecimals(flatElectricityPrice),
    valleyPrice: keepTwoDecimals(valleyElectricityPrice),
    totalElectricityPrice: keepTwoDecimals(totalElectricityPrice),

    // 电量占比（%）
    sharpUsageRate: keepTwoDecimals(sharpUsageRate),
    peakUsageRate: keepTwoDecimals(peakUsageRate),
    flatUsageRate: keepTwoDecimals(flatUsageRate),
    valleyUsageRate: keepTwoDecimals(valleyUsageRate),

    // 电费占比（%）
    sharpPriceRate: keepTwoDecimals(sharpPriceRate),
    peakPriceRate: keepTwoDecimals(peakPriceRate),
    flatPriceRate: keepTwoDecimals(flatPriceRate),
    valleyPriceRate: keepTwoDecimals(valleyUsageRate)
  };
}